package com.xafc.demo.framework.config;

import cn.hutool.core.util.StrUtil;
import com.xafc.demo.framework.filter.ParamsFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: ZhangMing
 * @Date: 2021/3/29 15:05
 */

@Configuration
public class ApplicationWebConfig implements WebMvcConfigurer {

    /**
     * 设置跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**");
    }


    /**
     * 格式化器
     * @param registry
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        /**
         * 日期时间格式化
         */
        registry.addConverter(new Converter<String, Date>() {
            private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            @Override
            public Date convert(String s) {
                if(StrUtil.isEmpty(s)){
                    return null;
                }
                try {
                    return simpleDateFormat.parse(s);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });

        /**
         * 时间格式化
         */
        registry.addConverter(new Converter<String, Date>() {
            private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

            @Override
            public Date convert(String s) {
                if(StrUtil.isEmpty(s)){
                    return null;
                }
                try {
                    return simpleDateFormat.parse(s);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
    }

    /**
     * Controller注册器
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
//        registry.addViewController("/" ).setViewName("redirect:/index");
        registry.addViewController("/login").setViewName("login");
    }

    /**
     * 去除传入参数的前后空格
     */
    @Bean
    public FilterRegistrationBean paramsFilterRegistration() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setDispatcherTypes(DispatcherType.REQUEST);
        registration.setFilter(new ParamsFilter());
        registration.addUrlPatterns("/*");
        registration.setName("paramsFilter");
        registration.setOrder(Integer.MAX_VALUE - 1);
        return registration;
    }

}

